package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;

    /**
     * 员工分页查询
     *
     * @param param
     * @return
     */
    @Override
    public PageResult getPageList(EmpQueryParam param) {

        Page<Emp> pageParam = new Page<>(param.getPage(), param.getPageSize());

        // 根据最后修改时间倒序排序
        pageParam.addOrder(OrderItem.desc("update_time"));

        //添加分页条件
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(param.getName() != null && !param.getName().isEmpty(), Emp::getName, param.getName())
                .eq(param.getGender() != null, Emp::getGender, param.getGender())
                .between(param.getBegin() != null && param.getEnd() != null, Emp::getEntryDate, param.getBegin(), param.getEnd());

        pageParam = page(pageParam, wrapper);

        //获取员工对象集合
        List<Emp> records = pageParam.getRecords();

        //获取员工部门id集合
        List<Long> ids = records.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());


        if (ids.size() > 0) {
            //查找部门名称集合
            List<Dept> depts = deptService.listByIds(ids);

            //给每个员工的部门名称赋值

            records.forEach(emp -> {
                depts.forEach(dept -> {
                    if (emp.getDeptId() != null && emp.getDeptId().equals(dept.getId())) {
                        emp.setDeptName(dept.getName());
                    }
//                if(emp.getDeptId() != null){
//                    emp.setDeptName(dept.getName());
//                }
                });
            });

        }


        return new PageResult(pageParam.getTotal(), records);
    }

    /**
     * 新增员工基本信息和经历信息
     *
     * @param emp
     */
    @Transactional
    @Override
    public void saveEmp(Emp emp) {
        //新增员工基本信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);//mp会把id返回注入到对象中

        //新增员工经历
        //给每个员工经历添加员工id
        List<EmpExpr> exprList = emp.getExprList();
        Long id = emp.getId();
        exprList.stream().forEach(empExpr -> empExpr.setEmpId(id));

        //批量添加员工经历
        empExprService.saveBatch(exprList);
    }

    /**
     * 根据id查询员工基本信息和经历信息
     *
     * @param id
     * @return
     */
    @Override
    public Emp getEmpById(Integer id) {

        //查询员工基本信息
        Emp emp = getById(id);

        //查询员工经历信息
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,id);
        List<EmpExpr> list = empExprService.list(wrapper);

        emp.setExprList(list);

        return emp;

    }

    /**
     * 修改员工信息与经历
     *
     * @param emp
     */
    @Transactional
    @Override
    public void updateEmpById(Emp emp) {
        //员工id
        Long id = emp.getId();

        //更新员工基本信息
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Emp::getId, id);
        //更新操作时间
        emp.setUpdateTime(LocalDateTime.now());
        update(emp,wrapper);


        //删除员工经历信息
        LambdaQueryWrapper<EmpExpr> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(EmpExpr::getEmpId,id);
        empExprService.remove(wrapper1);

        //批量新增经历,由于前端没有给经历添加员工id,我们自己添加

        List<EmpExpr> exprList = emp.getExprList();

        //如果员工经历为空,则结束方法
        if (exprList == null || exprList.size()<=0){
            return;
        }
        exprList.forEach(expr -> {
            expr.setEmpId(id);
        });

        //批量新增员工经历
        empExprService.saveBatch(exprList);

    }

    /**
     * 根据ids批量删除员工和经历
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteEmpByIds(List<Integer> ids) {
        //删除员工信息
        removeBatchByIds(ids);

        //删除经历信息
        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids);
        empExprService.remove(wrapper);
    }
}
